package cn.hzking.gjmall.net;

import android.content.Context;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import cn.hzking.gjmall.app.GjAppUtils;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.constant.Constants;
import cn.hzking.gjmall.net.config.HttpConfig;
import cn.hzking.gjmall.net.cookie.CookieManger;
import cn.hzking.gjmall.net.https.HttpsFfactory;
import cn.hzking.gjmall.net.intercept.InterceptorUtil;
import cn.hzking.gjmall.net.interfaces.ApiInterface;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



/**
 * Created by Administrator on 2018/1/20.
 */

public class RetrofitFactory {
    private final ApiInterface apiInterface;
    private static Context context;
    private RetrofitFactory(Context context){
        SSLContext sslContext = null;
        SSLSocketFactory sslSocketFactory=null;
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, null, new SecureRandom());
             sslSocketFactory = sslContext.getSocketFactory();
        } catch (Exception e) {
            e.printStackTrace();
        }
        OkHttpClient mOkHttpClient=new OkHttpClient.Builder()
               // .retryOnConnectionFailure(false)//请求错误了,不重复请求(默认会重复请求)
                .cookieJar(new CookieManger(context))//持久化cookie
                .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置写入超时时间
                .addInterceptor(InterceptorUtil.tokenInterceptor(context))//添加其他拦截器
                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
               //.sslSocketFactory(HttpsFfactory.getSSLSocketFactory(context,0))//添加https证书(第二个参数是)
              // .hostnameVerifier(HttpsFfactory.getHostnameVerifier("")) //过滤url,添加hostName验证器
                /*.sslSocketFactory(sslSocketFactory, new X509TrustManager() {//忽略证书
                    @Override
                    public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                }).hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })*/
                .build();

        Retrofit mRetrofit=new Retrofit.Builder()
                .baseUrl(Constants.isDebug? Gjmall.getDeBugApiHost():Gjmall.getrelaseApiHost())
                .addConverterFactory(GsonConverterFactory.create())//添加gson转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                .client(mOkHttpClient)
                .build();

        apiInterface = mRetrofit.create(ApiInterface.class);
    }

    private static final class Holder{
        private static final Context context = GjAppUtils.getContext();
        private static final RetrofitFactory RETROFITFACTORY = new RetrofitFactory(context);
    }
    public static final RetrofitFactory getInstant(){
        return Holder.RETROFITFACTORY;
    }

    public ApiInterface API(){
        return apiInterface;
    }



}
